// pages/task/index.js
Page({
  data: {
    userInfo: {
      level: 15,
      experience: 75,
      points: 1280
    },
    tasks: [
      {
        id: 1,
        title: "每日签到",
        description: "每天登录城旅社区",
        reward: 10,
        type: "daily",
        status: "completed",
        progress: 1,
        maxProgress: 1
      },
      {
        id: 2,
        title: "发布动态",
        description: "分享一条城市生活动态",
        reward: 20,
        type: "daily",
        status: "available",
        progress: 0,
        maxProgress: 1
      },
      {
        id: 3,
        title: "评论互动",
        description: "对3条动态进行评论",
        reward: 15,
        type: "daily",
        status: "available",
        progress: 1,
        maxProgress: 3
      },
      {
        id: 4,
        title: "完善资料",
        description: "完善个人资料信息",
        reward: 50,
        type: "once",
        status: "completed",
        progress: 1,
        maxProgress: 1
      },
      {
        id: 5,
        title: "邀请好友",
        description: "邀请3位好友加入城旅社区",
        reward: 100,
        type: "weekly",
        status: "available",
        progress: 0,
        maxProgress: 3
      },
      {
        id: 6,
        title: "连续签到7天",
        description: "连续签到7天获得额外奖励",
        reward: 200,
        type: "weekly",
        status: "available",
        progress: 3,
        maxProgress: 7
      }
    ],
    rewards: [
      { id: 1, name: "城旅徽章", cost: 100, image: "/assets/img/profile/medal.png", description: "展示你的城旅身份" },
      { id: 2, name: "个性头像框", cost: 200, image: "/assets/img/profile/card.png", description: "让你的头像更有个性" },
      { id: 3, name: "专属称号", cost: 300, image: "/assets/img/profile/tag.png", description: "获得专属用户称号" },
      { id: 4, name: "高级滤镜", cost: 150, image: "/assets/img/message/draw.png", description: "拍照时使用高级滤镜" }
    ]
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '任务中心'
    })
  },

  onTaskTap: function(e) {
    const task = e.currentTarget.dataset.task
    
    if (task.status === "completed") {
      wx.showToast({
        title: '任务已完成',
        icon: 'none'
      })
      return
    }
    
    wx.showModal({
      title: task.title,
      content: `描述：${task.description}\n奖励：${task.reward}积分\n进度：${task.progress}/${task.maxProgress}\n\n是否立即完成？`,
      showCancel: true,
      cancelText: '取消',
      confirmText: '完成',
      success: function(res) {
        if (res.confirm) {
          wx.showToast({
            title: '任务完成，获得' + task.reward + '积分',
            icon: 'success'
          })
        }
      }
    })
  },

  onRewardTap: function(e) {
    const reward = e.currentTarget.dataset.reward
    
    if (this.data.userInfo.points < reward.cost) {
      wx.showToast({
        title: '积分不足',
        icon: 'none'
      })
      return
    }
    
    wx.showModal({
      title: '兑换奖励',
      content: `确定要兑换"${reward.name}"吗？\n消耗积分：${reward.cost}\n描述：${reward.description}`,
      showCancel: true,
      cancelText: '取消',
      confirmText: '兑换',
      success: function(res) {
        if (res.confirm) {
          wx.showToast({
            title: '兑换成功',
            icon: 'success'
          })
        }
      }
    })
  }
})
